const userModel = require('../../model/UserModel')
const sqlCondition = require('../../utils/sqlCondition')
const db = require('../../config/db')
const {QueryTypes} = require("sequelize");

class UserSer {


    async addUser(userData) {
        userData.userRole = '1'
        userData = sqlCondition(userData)
        const data = await userModel.create(userData, {
            raw: true
        })
        return data
    }

    // 查询
    async getUserOne(userData) {
        const data = await userModel.findAll({
            where: {
                user_number: userData.userNumber,
            },
            attributes: [
                ['id', 'id'],
                ['user_number', 'userNumber'],
                ['user_name', 'userName'],
                ['user_sex', 'userSex'],
                ['user_role', 'userRole'],
                ['user_introduce', 'userIntroduce'],
                ['user_photo_url', 'userPhotoUrl'],
                ['user_blacklist', 'userBlacklist'],
                ['user_phone', 'userPhone'],
                ['user_email', 'userEmail'],
                ['user_background_img', 'userBackgroundImg'],
                ['user_background_music', 'userBackgroundMusic'],
            ],
        })
        return data
    }

    //编辑用户
    async editUser(userData) {
        userData = sqlCondition(userData)
        const data = await userModel.update(userData, {
            where: {
                user_number: userData.user_number,
            },
        })
        return data
    }


    //编辑用户id
    async editUserId(userData, id) {
        const data = await userModel.update(userData, {
            where: {
                id: id
            }
        })
        return data
    }

    // 查询
    async getUserLIst(dataList) {
        const data = await userModel.findAll({
            where: dataList,
            attributes: [
                ['id', 'id'],
                ['user_number', 'userNumber'],
                ['user_name', 'userName'],
                ['user_sex', 'userSex'],
                ['user_role', 'userRole'],
                ['user_introduce', 'userIntroduce'],
                ['user_photo_url', 'userPhotoUrl'],
                ['user_phone', 'userPhone'],
                ['user_blacklist', 'userBlacklist'],
                ['user_email', 'userEmail'],
                ['user_background_img', 'userBackgroundImg'],
                ['user_background_music', 'userBackgroundMusic'],
                ['updatedAt', 'updatedAt'],
                ['createdAt', 'createdAt'],
            ],
        })
        return data
    }

    //查询性别的数量
    async getUserListBySex(sex) {
        let SQL
        if (!sex) {
            SQL = 'SELECT COUNT(*) as count FROM blog_users WHERE user_sex is null'
        } else {
            SQL = `SELECT COUNT(*) as count FROM blog_users WHERE user_sex='${sex}'`
        }
        const data = await db.query(SQL, {type: QueryTypes.SELECT})
        return data
    }


}


module.exports = new UserSer()
